<?php
declare (strict_types=1);

namespace app\course\service\note\admin;

use app\common\service\BaseService;
use app\course\logic\note\admin\Note as NoteLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class Note
 *
 * @package app\course\service\note\admin
 */
class Note extends BaseService
{
    /**
     * @Inject()
     *
     * @var NoteLogic
     */
    protected $noteLogic;

    /**
     * 课程笔记列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/21
     *
     * @param int $courseId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function noteList(int $courseId)
    {
        // 搜索功能
        $map   = [
            'course_id' => $courseId
        ];
        $phone = Request::get('phone/s', '');
        $data  = $this->noteLogic->getList($map, $phone)->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        // 定义字段
        $keyList = [
            'user'        => ListItem::make('custom')->title('用户'),
            'content'     => ListItem::make()->title('内容'),
            'open_status' => ListItem::make('custom')->title('公开状态'),
            'create_time' => ListItem::make()->title('记录时间')
        ];

        $searchKey = [
            'phone' => FormItem::make('input')->title(false)->placeholder('学员手机号')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 删除笔记
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/21
     *
     * @param int $courseId
     * @param int $noteId
     * @return \think\Response
     */
    public function deleteNote(int $courseId, int $noteId)
    {
        $this->noteLogic->deleteNote($courseId, $noteId);

        return $this->parseToData([], 1, '删除成功');
    }
}